package io.renren.modules.projectinfo.service.impl;

import io.renren.common.utils.ConstantUtils;
import io.renren.common.utils.R;
import io.renren.modules.areainfos.service.AreainfosService;
import io.renren.modules.footprint.dao.VipfootprintinfosDao;
import io.renren.modules.footprint.service.VipfootprintinfosService;
import io.renren.modules.projectinfo.entity.ProjectinfosCustomEntity;
import io.renren.modules.user.dao.UserinfosDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

import io.renren.modules.projectinfo.dao.ProjectinfosDao;
import io.renren.modules.projectinfo.entity.ProjectinfosEntity;
import io.renren.modules.projectinfo.service.ProjectinfosService;



/**
 * @author 黄清
 */
@Service("projectinfosService")
public class ProjectinfosServiceImpl implements ProjectinfosService {



	@Autowired
	private ProjectinfosDao projectinfosDao;

	@Autowired
	private VipfootprintinfosDao vipfootprintinfosDao;

	@Autowired
	private UserinfosDao userinfosDao;

	@Autowired
	private AreainfosService areainfosService;

	@Autowired
	private VipfootprintinfosService vipfootprintinfosService;

	@Override
	public int apiSoftDelete(Map<String, Object> map) {
		int i = projectinfosDao.apiSoftDelete(map);
		return i;
	}

	@Override
	public List<ProjectinfosCustomEntity> queryApiMyList(Map<String, Object> map) {
		return projectinfosDao.queryApiMyList(map);
	}

	@Override
	public int queryApiMyTotal(Map<String, Object> map) {
		return projectinfosDao.queryApiMyTotal(map);
	}

	@Override
	public int deleteByUser(String[] createuser) {
		return projectinfosDao.deleteByUser(createuser);
	}

	@Override
	public int addViewTimes(String projectinfoid) {
		return projectinfosDao.addViewTimes(projectinfoid);
	}

	@Override
	public int addCollectTimes(String projectinfoid) {
		return projectinfosDao.addCollectTimes(projectinfoid);
	}

	@Override
	public int minusCollectTimes(String projectinfoid) {
		return projectinfosDao.minusCollectTimes(projectinfoid);
	}

	@Override
	public int apiDeletes(Map<String, Object> map) {
		return projectinfosDao.apiDeletes(map);
	}

	@Override
	public int queryProgress(String projectinfoid) {
		return projectinfosDao.queryProgress(projectinfoid);
	}

	@Override
	public int queryDescTotal(Map<String, Object> map) {
		return projectinfosDao.queryDescTotal(map);
	}

	@Override
	public List<ProjectinfosCustomEntity> queryDescList(Map<String, Object> map) {
		return projectinfosDao.queryDescList(map);
	}

	@Override
	public List<ProjectinfosCustomEntity> queryApiList(Map<String, Object> map) {
		return projectinfosDao.queryApiList(map);
	}

	@Override
	public ProjectinfosCustomEntity queryApiInfo(String projectinfoid) {
		ProjectinfosCustomEntity projectinfosCustomEntity = projectinfosDao.queryApiInfo(projectinfoid);
		if(projectinfosCustomEntity == null){
		    return null;
        }
		//更新浏览次数
		projectinfosDao.addViewTimes(projectinfoid);
		return projectinfosCustomEntity;
	}


	@Override
	public int queryApiTotal(Map<String, Object> map) {
		return projectinfosDao.queryApiTotal(map);
	}

	@Override
	public void releaseApiProject(ProjectinfosEntity projectinfosEntity) {
		//判断当前用户是否有权限进行操作
		//设置项目id
		String id = UUID.randomUUID().toString().replaceAll("-", "");
		projectinfosEntity.setProjectinfoid(id);
		//设置软删除
		projectinfosEntity.setIsdel(0);
		//设置浏览次数
		projectinfosEntity.setViewtimes(0);
		//设置收藏次数
		projectinfosEntity.setCollecttimes(0);
		//设置创建时间
		Date date = new Date();
		projectinfosEntity.setCreatetime(date);
		//设置更新时间
		projectinfosEntity.setUpdatetime(date);

		//审核方面
		//设置审核状态（防止用户传入）
		projectinfosEntity.setAuditstatus(ConstantUtils.AUDIT);
		projectinfosEntity.setDealuser(null);
		projectinfosEntity.setAuditdate(null);
		projectinfosEntity.setCannotpassreason(null);
		projectinfosEntity.setProjectprogress(ConstantUtils.PROGRESSING);
		projectinfosDao.save(projectinfosEntity);
	}

	//无法修改类型
	@Override
	public void updateApiProject(ProjectinfosEntity projectinfosEntity) {
		//设置数据
		//设置更新时间
		Date date = new Date();
		projectinfosEntity.setUpdatetime(date);
		//设置审核状态为待审核
		projectinfosEntity.setAuditstatus(ConstantUtils.AUDIT);
		projectinfosDao.updateApiProject(projectinfosEntity);
	}

	@Override
	public R isProjectExistByUser(Map<String, Object> params) {
		int projectExistByUser = projectinfosDao.isProjectExistByUser(params);
		if(projectExistByUser == 0){
			return R.error("您没有此项目!");
		}
		return null;
	}

	@Override
	public List<ProjectinfosCustomEntity> queryAuditList(Map<String, Object> map) {
		return projectinfosDao.queryAuditList(map);
	}

	@Override
	public int auditProject(ProjectinfosEntity projectinfos) {
		return projectinfosDao.auditProject(projectinfos);
	}

	@Override
	public ProjectinfosCustomEntity queryAuditInfo(String projectinfoid) {
		return projectinfosDao.queryAuditInfo(projectinfoid);
	}

	@Override
	public int queryAuditTotal(Map<String, Object> map) {
		return projectinfosDao.queryAuditTotal(map);
	}

	@Override
	public ProjectinfosEntity queryObject(String projectinfoid){
		return projectinfosDao.queryObject(projectinfoid);
	}

	@Override
	public ProjectinfosCustomEntity queryCustomInfo(String projectinfoid) {
		return projectinfosDao.queryCustomInfo(projectinfoid);
	}

	@Override
	public List<ProjectinfosEntity> queryList(Map<String, Object> map){
		return projectinfosDao.queryList(map);
	}

	@Override
	public List<ProjectinfosCustomEntity> queryCustomList(Map<String, Object> map) {
		return projectinfosDao.queryCustomList(map);
	}

	@Override
	public int queryTotal(Map<String, Object> map){
		return projectinfosDao.queryTotal(map);
	}
	
	@Override
	public void save(ProjectinfosEntity projectinfos){
		projectinfosDao.save(projectinfos);
	}
	
	@Override
	public void update(ProjectinfosEntity projectinfos){
		projectinfosDao.update(projectinfos);
	}
	
	@Override
	public void delete(String projectinfoid){
		projectinfosDao.delete(projectinfoid);
	}
	
	@Override
	public void deleteBatch(String[] projectinfoids){
		projectinfosDao.deleteBatch(projectinfoids);
	}

	@Override
	public R isProjectIdExist(String projectinfoids) {
		int projectIdExist = projectinfosDao.isProjectIdExist(projectinfoids);
		if(projectIdExist == 0){
			return R.error("项目不存在！");
		}
		return null;
	}

	@Override
	public R judgeAuditError(ProjectinfosEntity projectinfos,Long userid){
		//获取审核标识
		Integer auditstatus = projectinfos.getAuditstatus();
		//验证审核标识
		if(auditstatus != ConstantUtils.AUDIT && auditstatus != ConstantUtils.AUDITED && auditstatus != ConstantUtils.AUDITFAILURE){
			return R.error("审核标识错误！");
		}
		//审核失败：1.设置审核时间，2.设置审核人，3.设置不通过理由
		if(auditstatus == ConstantUtils.AUDITFAILURE){
			//审核失败，却没有理由
			if(projectinfos.getCannotpassreason() == null || "".equals(projectinfos.getCannotpassreason())) {
				return R.error("请填写不通过理由！");
			}

			//通过处理人
			projectinfos.setDealuser(userid);
		}
		//审核通过：1.设置审核时间，2.设置审核人，3.清空数据库总不通过理由
		if(auditstatus == ConstantUtils.AUDITED){
			//数据库中清空理由
			if(projectinfos.getCannotpassreason() != null || !"".equals(projectinfos.getCannotpassreason())) {
				projectinfos.setCannotpassreason("");
			}
			//设置日期
			projectinfos.setAuditdate(new Date());
			//通过处理人
			projectinfos.setDealuser(userid);
		}
		//待审核：1.置空处理人，2.置空审核时间，3.置空不同原因
		if(auditstatus == ConstantUtils.AUDIT){
			//置空理由
			projectinfos.setCannotpassreason(null);
			//指控处理人
			projectinfos.setDealuser(null);
		}
		//设置日期
		projectinfos.setAuditdate(new Date());
		return null;
	}
	@Override
	public R judgeProgressError(int code){
		//项目进度标识判断
		if(code != ConstantUtils.PROGRESSING && code != ConstantUtils.PROGRESSED){
			return R.error("项目进度标识错误！");
		}
		return null;
	}

	@Override
	public R judgeProjectTypeError(int code) {
		if(code != ConstantUtils.PROJECTTYPE && code != ConstantUtils.MOTORCADE){
			return R.error("项目类型标识错误!");
		}
		return null;
	}

	@Override
	public R judgeIsOperationProject(String projectinfoid) {
		//项目存在判断（包含软删除）
		int isDel = projectinfosDao.isProjectIdExist(projectinfoid);
		if(isDel != ConstantUtils.DELED){
			return R.error("项目已删除，无法访问！");
		}
		//项目审核通过判断
		int auditstatus = projectinfosDao.queryAuditStatus(projectinfoid);
		if(auditstatus == ConstantUtils.AUDIT || auditstatus == ConstantUtils.AUDITFAILURE){
			return R.error("项目审核中，稍后操作");
		}
		return null;
	}

	@Override
	public R judgeIsDelError(int code) {
		if(code != ConstantUtils.DEL && code != ConstantUtils.DELED){
			return R.error("软删除状态码错误!");
		}
		return null;
	}

	@Override
	public String queryConnectionInfo(String projectinfoid) {
		return projectinfosDao.queryConnectionInfo(projectinfoid);
	}

	@Override
	public R judgeProjectAuditSucceed(String projectinfoid) {
		int auditstatus = projectinfosDao.queryAuditStatus(projectinfoid);
		if(auditstatus == ConstantUtils.AUDIT || auditstatus == ConstantUtils.AUDITFAILURE){
			return R.error("等待项目审核，无法操作");
		}
		return null;
	}


}
